package com.agileboot.domain.airport.selfcheck.db;

import com.agileboot.common.core.page.PageDTO;
import com.agileboot.domain.airport.selfcheck.command.AddSirSelfCheckCommand;
import com.agileboot.domain.airport.selfcheck.query.SirSelfCheckQuery;
import com.agileboot.domain.airport.selfcheck.dto.SirSelfCheckDTO;
import com.agileboot.domain.system.dept.DeptApplicationService;
import com.agileboot.domain.system.dept.db.SysDeptEntity;
import com.agileboot.domain.system.dept.db.SysDeptService;
import com.agileboot.domain.system.tag.db.SysTagEntity;
import com.agileboot.domain.system.tag.db.SysTagServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class SirSelfCheckServiceImpl extends ServiceImpl<SirSelfCheckMapper, SirSelfCheckEntity> {


@Resource
private  SirSelfCheckMapper mapper;

    @Resource
    private SysTagServiceImpl sysTagService;

    // 添加SysDeptService的依赖注入
    @Resource
    private SysDeptService sysDeptService;


    public PageDTO<SirSelfCheckDTO> getPageList(SirSelfCheckQuery query) {
        Page<SirSelfCheckEntity> page = this.page(query.toPage(), query.toQueryWrapper());
        List<SirSelfCheckDTO> records = convertToDTOs(page.getRecords());
        return new PageDTO<>(records, page.getTotal());
    }

    public List<SirSelfCheckDTO> getList(SirSelfCheckQuery query) {
        List<SirSelfCheckEntity> all = this.list(query.toQueryWrapper());
        return convertToDTOs(all);
    }

    public SirSelfCheckDTO getInfo(Long id) {
        SirSelfCheckEntity entity = this.getById(id);
        if (entity == null) {
            return null;
        }
        List<SirSelfCheckDTO> dtos = convertToDTOs(Collections.singletonList(entity));
        return dtos.isEmpty() ? null : dtos.get(0);
    }



    public void saveOrUpdateSirSelfCheck(AddSirSelfCheckCommand bo) {
        SirSelfCheckEntity entity = new SirSelfCheckEntity();
        BeanUtils.copyProperties(bo, entity);
        entity.setUpdateTime(new Date());
        if (entity.getCreateTime() == null) {
            entity.setCreateTime(new Date());
        }
        this.saveOrUpdate(entity);
    }


    public void deleteSirSelfCheck(List<Long> ids) {
        this.removeBatchByIds(ids);
    }

    public List<SirSelfCheckEntity> getByIds(List<Long> ids) {
        return this.lambdaQuery()
                .in(SirSelfCheckEntity::getId, ids)
                .list();
    }


    /**
     * 将实体列表转换为DTO列表，并为belongDeptName和tagName字段赋值
     * @param entities 实体列表
     * @return DTO列表
     */
    public List<SirSelfCheckDTO> convertToDTOs(List<SirSelfCheckEntity> entities) {
        if (entities.isEmpty()) {
            return Collections.emptyList();
        }

        // 收集所有需要查询的部门ID和标签ID
        Set<Long> deptIds = new HashSet<>();
        Set<Integer> tagIds = new HashSet<>();

        for (SirSelfCheckEntity entity : entities) {
            if (entity.getBelongDept() != null) {
                deptIds.add(entity.getBelongDept());
            }
            if (entity.getTagId() != null) {
                tagIds.add(entity.getTagId().intValue());
            }
        }

        // 批量查询部门信息和标签信息
        Map<Long, String> deptNameMap = new HashMap<>();
        Map<Integer, String> tagNameMap = new HashMap<>();

        // 查询部门名称 - 使用SysDeptService的getByIds方法
        if (!deptIds.isEmpty()) {
            List<SysDeptEntity> deptEntities = sysDeptService.getByDeptIds(new ArrayList<>(deptIds));
            deptNameMap = deptEntities.stream()
                    .collect(Collectors.toMap(SysDeptEntity::getDeptId, SysDeptEntity::getDeptName));
        }

        // 查询标签名称
        if (!tagIds.isEmpty()) {
            List<SysTagEntity> tags = sysTagService.getByIds(new ArrayList<>(tagIds));
            tagNameMap = tags.stream()
                    .collect(Collectors.toMap(SysTagEntity::getTagId, SysTagEntity::getTagName));
        }

        // 转换为DTO并设置部门名称和标签名称
        List<SirSelfCheckDTO> dtos = new ArrayList<>();
        for (SirSelfCheckEntity entity : entities) {
            SirSelfCheckDTO dto = new SirSelfCheckDTO();
            BeanUtils.copyProperties(entity, dto);

            // 设置所属部门名称
            if (entity.getBelongDept() != null && deptNameMap.containsKey(entity.getBelongDept())) {
                dto.setBelongDeptName(deptNameMap.get(entity.getBelongDept()));
            }

            // 设置标签名称
            if (entity.getTagId() != null && tagNameMap.containsKey(entity.getTagId().intValue())) {
                dto.setTagName(tagNameMap.get(entity.getTagId().intValue()));
            }

            dtos.add(dto);
        }

        return dtos;
    }

    /**
     * 批量更新安全检查的归属部门
     * @param ids 安全检查ID列表
     * @param belongDept 归属部门ID
     */
    public void batchUpdateBelongDept(List<Long> ids, Long belongDept) {
        this.lambdaUpdate()
                .set(SirSelfCheckEntity::getBelongDept, belongDept)
                .set(SirSelfCheckEntity::getUpdateTime, new Date())
                .in(SirSelfCheckEntity::getId, ids)
                .update();
    }

    /**
     * 批量更新安全检查的标签
     * @param ids 安全检查ID列表
     * @param tagId 标签ID
     */
    public void batchUpdateTagId(List<Long> ids, Long tagId) {
        this.lambdaUpdate()
                .set(SirSelfCheckEntity::getTagId, tagId)
                .set(SirSelfCheckEntity::getUpdateTime, new Date())
                .in(SirSelfCheckEntity::getId, ids)
                .update();
    }

}
